﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.Drawing;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;

namespace Phong_CTSV
{
    public partial class UCViewTable1 : System.Web.UI.UserControl
    {
        Form form = null;
        String formName = null;
        long uni = 0;
        List<Columns_Header> columnsHeader = new List<Columns_Header>();
        List<Rows_Header> rowsHeader = new List<Rows_Header>();
        List<Columns_Header> allcolumns = new List<Columns_Header>();
        List<Columns_Header> childColumnsHeader = new List<Columns_Header>();
        Data_Table[,] dataTable = null;
        public Data_Table[] Sum = null;
        int numColumn = 0;
        int numRow = 0;
        QuerrySQL query = new QuerrySQL();
        int year = 2011;
        University u;

        String userName;

        public int[] addID = new int[] { 2, 3, 4, 5, 6, 7, 8 };
    

        public UCViewTable1()
            : base()
        {

        }

        public Form formInfo
        {
            get
            {
                return form;
            }
        }

        public List<Columns_Header> ColumnsHeaderList
        {
            get
            {
                return columnsHeader;
            }
        }

        public List<Rows_Header> RowsHeaderList
        {
            get
            {
                return rowsHeader;
            }
        }

        public Data_Table[,] DataTable
        {
            get
            {
                return dataTable;
            }
        }

        public int NumRow
        {
            get
            {
                return numRow;
            }
        }

        public int NumColumn
        {
            get
            {
                return numColumn;
            }
        }

        public String FormName
        {
            get
            {
                return formName;
            }

            set
            {
                formName = value;

            }
        }
        public Data_Table[] SumValue
        {
            get { return Sum; }
        }
        private void getNumRowAndColumn()
        {
            numColumn = childColumnsHeader.Count;
            if (form.Has_rows)
            {
                foreach (Rows_Header rh in rowsHeader)
                {
                    if (!rh.Has_child)
                    {
                        numRow++;
                    }

                }
                numColumn--;
            }
            else
            {
                numRow = query.maxRow(form.Name, u.Name, year);
            }




        }
        Data_Table[] getSum()
        {
            Data_Table[] _Sum = new Data_Table[numColumn];
            long total = 0;
            try
            {
                for (int i = 0; i < numColumn; i++)
                {
                    total = 0;
                    _Sum[i] = new Data_Table();
                    for (int j = 0; j < numRow; j++)
                    {
                        total += long.Parse(dataTable[j, i].Value);//
                    }
                    _Sum[i].Value = total.ToString();


                }
            }
            catch
            {
             for (int i = 0; i <numColumn; i++)
                 _Sum[i] = new Data_Table();
            }
            return _Sum;
        }
        private void getDataTable(long uni)
        {
            dataTable = new Data_Table[numRow, numColumn];
            try
            {
                for (int i = 0; i <numColumn; i++)
                {
                    for (int j = 0; j < numRow; j++)
                    {
                        dataTable[j, i] = query.Select_Data_Cell(form.Id, uni, (int)childColumnsHeader[i].ID, j, year);
                    }
                }
            }
            catch
            {
                for (int i = 0; i <numColumn; i++)
                {
                    for (int j = 0; j < numRow; j++)
                    {
                        dataTable[j, i] = new Data_Table();
                    }
                }
            }
        }
        public string NameTable
        {
            get;
            set;
        }
        public bool IsSum = false;
    

        //----------------
        protected void Page_Load(object sender, EventArgs e)
        {
           if (Session["userName"] != null)
            {
                u = query.Select_UniversityByUser(Session["userName"].ToString());
                uni = u.ID;
                userName = Session["userName"].ToString();
                formName = "Bảng 1";// +Request["Form"];
                form = query.Select_Form(formName);

                year = System.DateTime.Now.Year;
                rowsHeader = query.Select_RowsHeaderByForm(formName);
                allcolumns = query.Select_ColumnsHeaderByForm(formName);
                for (int i = 0; i < addID.Length; i++)
                {
                        columnsHeader.Add(query.Select_ColumnHeaderByID(form.Id,addID[i]));
                    
                }
                foreach (var item in columnsHeader)
                {
                    if (item.Parent_id == 0)
                    {
                        if (!item.Has_child)
                            childColumnsHeader.Add(item);
                        else
                        {
                            foreach (var child in columnsHeader)
                            {
                                if (child.Parent_id == item.ID && !child.Has_child)
                                    childColumnsHeader.Add(child);
                            }
                        }
                    }
                }
                getNumRowAndColumn();
                getDataTable(uni);
                Sum = getSum();
            }
            else
            {
                Response.Redirect("~/LoginPage.aspx");
            }

        }

      

      
   

    }
}